<template lang="pug">
div
  FixedScroll
    div(slot="fixed")
      PageHeader(pageTitle="出门证扫描")

      ScanSearch(
          placeholder="出门证"
          :initSearchValue="searchValue"
          @search="search"
          @scan="search"
          )

      g-v.info(v-if="info")
        KvPair(k="出门证编号" :v="OutPermitNo")
        KvPair(
            v-if="info.OrderType && PermitType != '2'"
            k="单据类型"
            :v="info.OrderType"
            )
        KvPair(v-if="info.OutTime" k="出门时间" :v="info.OutTime | Date")
        KvPair(v-if="info.Corporation" k="出门单位" :v="info.Corporation")
        KvPair(v-if="info.OutReason" k="出门原因" :v="info.OutReason")

    StatTable.table(
        slot="scroll"
        :headList="headList"
        :body="body"
        :isLoading="isLoading"
        )

          //- v-if="$root.User.permMap['SaveToPicking(按单出库)']"
    g-h.bottom-bar(slot="fixed-bottom" j-c="space-around")
      Button(
          @click="pass(true)"
          text="放行"
          :disabled="passBtn.disabled"
          )
      Button(
          @click="pass(false)"
          text="不放行"
          :disabled="noPassBtn.disabled"
          )

</template>

<script>
import api from "$api"
import util from "$util"

export default {
  data() {
    return {
      searchValue: "",
      isLoading: false,
      saveEnabled: false,

      loading: {
        search: false,
        pass: false,
      },

      passBtn: {
        disabled: true,
      },
      noPassBtn: {
        disabled: true,
      },

      PermitType: "",
      OutPermitNo: "",

      info: null,
      headList: [
        { field: "PartNo", title: "物料编号", checked: true },
        { field: "PartName", title: "物料名称", checked: true },
        { field: "Unit", title: "单位", checked: true },
        { field: "Num", title: "数量", checked: true },
      ],
      body: {
        list: [],
      },
    }
  },
  methods: {
    async search(value) {
      if (this.loading.search) {
        return
      }
      if (value === "") {
        return
      }

      this.loading.search = true
      try {
        let data = await api.EntryScanSearch({
          PermitNo: value,
        })
        if (data.OutPermitHeadInfo) {
          switch (data.OutPermitHeadInfo.OrderType) {
            case 1:
              data.OutPermitHeadInfo.OrderType = "退货"
              break
            case 2:
              data.OutPermitHeadInfo.OrderType = "小票"
              break
            case 3:
              data.OutPermitHeadInfo.OrderType = "器具"
              break
            case 4:
              data.OutPermitHeadInfo.OrderType = "非器具"
              break
          }
        }
        this.info = data.OutPermitHeadInfo
        this.body.list = data.OutPermitDetailList
        this.PermitType = data.PermitType
        this.OutPermitNo = data.OutPermitNo

        this.passBtn.disabled = false
        this.noPassBtn.disabled = false
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.search = false
      }
    },

    // 允许/不允许出门
    async pass(canPass) {
      if (this.loading.pass) {
        return
      }
      if (!this.info) {
        return
      }

      this.loading.pass = true
      try {
        if (canPass) {
          await api.EntryScanPass({
            PermitNo: this.OutPermitNo,
            PermitType: this.PermitType,
          })
          util.Toast("放行成功")
        } else {
          await api.EntryScanNoPass({
            PermitNo: this.OutPermitNo,
            PermitType: this.PermitType,
          })
          util.Toast("不放行成功")
        }
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.pass = false
      }
    },

    registerScanner() {
      util.registerScanner((data) => {
        this.searchValue = data
        this.search(data)
      })
    },
  },

  created() {
    this.registerScanner()
  },
}
</script>

<style lang="stylus" scoped>

.save-disabled
  color: grey500

.active:active
  active-color()

.table
  scan-table()

.info
  border: 1px solid grey200

.bottom-bar
  bottom: 10rem
  bottom-bar()

</style>
